package com.fingard.dsp.bank.directbank.weixin01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;
import com.fingard.net.ConnState;
import com.fingard.xml.XmlTextReader;

import java.util.ArrayList;

public class WEIXIN01CloseOrder extends WEIXIN01Base implements ICloseOrder {
    public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req) {
        RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
        retAts.rdSeq = p_req.rdSeq;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
            if (actItem == null) {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]对应的商户配置信息!";
                return retAts;
            }

            ArrayList<String> arrayParams = new ArrayList<String>();

            StringBuilder sbXml = new StringBuilder();
            sbXml.append("<xml>\r\n");

            String appid = actItem.appId;
            sbXml.append("  <appid>" + appid + "</appid>\r\n");
            arrayParams.add("appid=" + appid);

            String mch_id = actItem.merchantId;
            sbXml.append("  <mch_id>" + mch_id + "</mch_id>\r\n");
            arrayParams.add("mch_id=" + mch_id);

            sbXml.append("  <out_trade_no>" + p_req.rdSeq + "</out_trade_no>\r\n");
            arrayParams.add("out_trade_no=" + p_req.rdSeq);

            String nonce_str = get_nonce_str();
            sbXml.append("  <nonce_str>" + nonce_str + "</nonce_str>\r\n");
            arrayParams.add("nonce_str=" + nonce_str);

            String[] signParams = new String[arrayParams.size()];
            arrayParams.toArray(signParams);
            String sign = this.signMessage(signParams, actItem.saltKey);
            sbXml.append("  <sign>" + sign + "</sign>\r\n");
            sbXml.append("</xml>");

            String[] tmpStrRet = sendToBank(sbXml.toString(), "closeorder");

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String return_code = tmpBkReader.getFirstText("/xml/return_code");
                    String return_msg = tmpBkReader.getFirstText("/xml/return_msg");
                    if (return_code != null && return_code.equals("SUCCESS")) {
                        String result_code = tmpBkReader.getFirstText("/xml/result_code");
                        retAts.respInfo = tmpBkReader.getFirstText("/xml/result_msg");
                        retAts.errorCode = tmpBkReader.getFirstText("/xml/err_code");
                        retAts.errorMsg = tmpBkReader.getFirstText("/xml/err_code_des");
                        retAts.respCode = "0";
                        if (result_code.equals("SUCCESS")) {
                            retAts.closeState = CloseOrderState.Close_Success;
                        } else if (result_code.equals("FAIL")) {
                            retAts.closeState = CloseOrderState.Close_Fail;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = return_msg;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.closeState = CloseOrderState.Close_Fail;
                retAts.respInfo = tmpStrRet[1];
                retAts.errorCode = tmpStrRet[0];
                retAts.errorMsg = tmpStrRet[1];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}